const express = require('express')

const app = express()

const whiteList = ['localhost'] // 127.0.0.1 无法访问
const preventHotLingKing = (req, res, next) => {
	const referer = req.headers.referer
	console.log(referer)
	if (referer) {
		const { hostname } = new URL(referer)
		if (!whiteList.includes(hostname)) {
			console.log(222)
			res.status(403).send('禁止访问')
			return
		}
	}

	next()
}

// TODO 防盗链
app.use(preventHotLingKing)

// TODO 初始化静态资源目录,
// FIXME 这个为什么要下use防盗链下面
app.use(express.static('static'))
// app.use('/assets',express.static("static")) // 也可以加前缀

app.listen(8000, () => {
	console.log('http://localhost:8000')
})
